package com.saishi.dao.impl;

import com.saishi.dao.ClassesDao;
import com.saishi.entity.Classes;
import com.saishi.util.AliPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;

public class ClassesDaoImpl implements ClassesDao {
    @Override
    public List<Classes> selectAll() throws SQLException {
        List<Classes> list=new ArrayList<Classes>();
        Connection conn= AliPool.getConn();
        String sql="select * from classes";
        PreparedStatement pst=conn.prepareStatement(sql);
        ResultSet res=pst.executeQuery();
        while(res.next()){
            Classes c=new Classes(res.getInt(1),res.getString(2),res.getString(3),res.getString(4),res.getDate(5),res.getInt(6));
            list.add(c);
        }
        return list;
    }

    //添加班级
    @Override
    public void add(String cname, String cgrade, String cstate, Date ccreatime, int teacher) throws SQLException {
        Connection conn=AliPool.getConn();
        String sql="insert into classes values(null,?,?,?,?,?)";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1,cname);
        pst.setString(2,cgrade);
        pst.setString(3,cstate);
        pst.setDate(4, ccreatime);
        pst.setInt(5,teacher);
        int num=pst.executeUpdate();
    }

    //删除班级
    @Override
    public void del(int id) throws SQLException {
        Connection conn=AliPool.getConn();
        String sql="delete from classes where cid=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1,id);
        int num=pst.executeUpdate();
    }

    @Override
    public List selectId() throws SQLException {
        List list =new ArrayList();
        Connection conn=AliPool.getConn();
        String sql="select * from classes";
        PreparedStatement pst=conn.prepareStatement(sql);
        ResultSet res=pst.executeQuery();
        while(res.next()){
            list.add(res.getInt(1));
        }
        return list;
    }

    //通过id查所有
    @Override
    public List selectById(int cid) throws SQLException {
        List list=new ArrayList();
        Connection conn=AliPool.getConn();
        String sql="select * from classes where cid=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1,cid);
        ResultSet res=pst.executeQuery();
        while(res.next()){
            Classes c=new Classes(res.getInt(1),res.getString(2),res.getString(3),res.getString(4),res.getDate(5),res.getInt(6));
            list.add(c);
        }
        return list;
    }

    //修改
    @Override
    public void Update(int cid, String cname, String cgrade, String cstate, Date ccreatime, int teacher) throws SQLException {
        Connection conn=AliPool.getConn();
        String sql="update classes set cname=?,cgrade=?,cstate=?,ccreatime=?,teacherid=? where cid=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1,cname);
        pst.setString(2,cgrade);
        pst.setString(3,cstate);
        pst.setDate(4,ccreatime);
        pst.setInt(5,teacher);
        pst.setInt(6,cid);
        int num=pst.executeUpdate();
    }

    public static void main(String[] args) throws SQLException {
//        List list=new ClassesDaoImpl().selectById(16);
//        System.out.println(list);
    }

}
